'use strict'

const Controller = require('egg').Controller

class DataController extends Controller {

  constructor(ctx) {
    super(ctx)
  }

  // 用户登入
  async index() {
    const { ctx, service } = this


    ctx.validate({ 
        pdi: {
            type: 'string', format: /^\d+$/, required: true, message: ctx.__('Illegal equipment number parameter')
        }
    }, ctx.params)

    const { pdi } = ctx.params


    const deviceInfo = await service.dataService.getDeviceInfo(pdi)


    if(!deviceInfo) {
        ctx.throw(422, ctx.__('The device does not exist'))
    }

    const { dt_rtdata_table, dt_hisdata_table } = deviceInfo.devicetype

    const startOfDay = ctx.helper.startOfDay()

    const [realData] = await service.dataService.getTableData(dt_rtdata_table, pdi)

    const historyData = await service.dataService.getTableData(dt_hisdata_table, pdi, startOfDay)

    //历史告警
    const historyWarn = []
    //const historyWarn = await service.dataService.getAlarmHistory(pdi, startOfDay)

    //实时告警
    const realWarn = await service.dataService.getAlarmReal(pdi)

    const data = { real: realData||null, history: historyData, historyWarn, realWarn }


    ctx.helper.success({ ctx, res: data })
  }


  //根据分类获取设备
  async devicesByType() {

    const { ctx, service } = this

    ctx.validate({ 
      typeid: {
          type: 'string', format: /^\d+$/, required: true, message: ctx.__('Illegal device classification parameter')
      }
  }, ctx.params)

    const { typeid } = ctx.params

    const devices = await service.dataService.getTypeDevice(typeid)

    const data = { devices }


    ctx.helper.success({ ctx, res: data })
  }

  //根据分类获取设备实时信息
  async devicesRealData() {

      const { ctx, service } = this
  
      ctx.validate({ 
        typeid: {
            type: 'string', format: /^\d+$/, required: true, message: ctx.__('Illegal device classification parameter')
        }
    }, ctx.params)
  
      let { typeid, pdi } = ctx.params


      const tables = { 
        33: { table: "t_realdata_TempHumi" }, 
        34: { table: "t_realdata_Switch" }
      }

      const { table } = tables[typeid] || {}

      if(!table) {
        ctx.throw(422, ctx.__('Device table does not exist'))
      }

      const realData = await service.dataService.getTypeRealData(typeid, table)

      const [ first ] = realData

      let historyData = []

      let historyWarn = []

      const startOfDay = ctx.helper.startOfDay()

      if(!pdi&&first) {
        pdi = first.pdi_index
      }

      if( typeid != 34) {
        historyData = await service.dataService.getTableData(table.replace('t_realdata', 't_hisdata'), pdi, startOfDay)
      }

      if( typeid == 34) {
        historyWarn = await service.dataService.getDayAlarmCount(pdi, startOfDay)
      }

      const realWarn = await service.dataService.getAlarmReal(typeid)

      //告警数量
      const pdiArr = realData.reduce((arr, item)=>{
        arr.push(item.pdi_index)
        return arr
      }, [])

      const alarmCount = await service.dataService.getDeviceAlarmCount(pdiArr)

  
      const data = { real: realData, history: historyData, realWarn, alarmCount, historyWarn: ctx.helper.formatHistoryWarn(historyWarn) }
  
  
      ctx.helper.success({ ctx, res: data })
    }

    //根据设备所有分类
    async devicetype() {

      const { ctx, service } = this

      const data = await service.dataService.deviceTypes()

      ctx.helper.success({ ctx, res: data })
    }
  
}

module.exports = DataController
